package ISearchblogtest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://62.234.216.147:8080/blog_content.html?id=",
                "这是自动化测试第二篇博客！",
                "一般是指软件测试的自动化，软件测试就是在预设条件下运行系统或应用程序，评估运行结果，预先条件应包括正常条件和异常条件。"
        ));
    }

    public static Stream<Arguments> Generator2() {
        return Stream.of(Arguments.arguments("http://62.234.216.147:8080/blog_list.html"));
    }

    /*
    * 输入正确的账号，密码, 验证码 登录成功
    * */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://62.234.216.147:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号jiaoao
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入验证码  (注：此处采用手工测试)
//        File imageFile = new File("D:/home/image/");
//        File[] files = imageFile.listFiles();
//        if (files != null && files.length > 0) {
//            Arrays.sort(files, Comparator.comparing(File::lastModified));
//            File lastImage = files[files.length - 1];
//            if (lastImage.getName().endsWith(".png")) {
//
//            } else {
//                System.out.println("没有找到当前要找的图片！");
//            }
//        } else {
//            System.out.println("当前文件夹为空！");
//        }
//        webDriver.findElement(By.cssSelector("#code_input")).sendKeys();
        sleep(9000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 跳转到列表页
        //获取到当前页面的 url 如果url 是正确的则测试通过
        sleep(1000);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(blog_list_url, cur_url);
        //列表页展示用户信息是jiaoao
        //用户名是 jiaoao 则测试通过
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
        String cur_admin = webDriver.findElement(By.cssSelector("#author2")).getText();
        Assertions.assertEquals(username, cur_admin);
        System.out.println("登录页面通过自动化测试");
    }

    // 测试异常登录情况
    @Test
    @ParameterizedTest
    @CsvFileSource(resources = "LoginFile.csv")
    void loginFile(String username, String password, String blog_list_url) throws InterruptedException {
        webDriver.get("http://62.234.216.147:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号jiaoao
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        sleep(9000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 跳转到列表页
        //获取到当前页面的 url 如果url 是正确的则测试通过
        sleep(1000);
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(blog_list_url, cur_url);
        System.out.println("登录页面失败通过自动化测试! [用户名或密码错误]");
    }


    // 测试验证码功能 (未完成)
//    @Test
//    @ParameterizedTest
//    @CsvFileSource(resources = "LoginFile.csv")
//    void blogCodeTest(String code) {
//        //获取博客登录页面
//        webDriver.get("http://localhost:8080/login.html");
//        //获取登录页面验证码元素
//        webDriver.findElement(By.cssSelector("#code_input")).sendKeys(code);
//        String cur_url = webDriver.getCurrentUrl();
//        Assertions.assertEquals("http://localhost:8080/myblog_list.html", cur_url);
//    }

    //博客列表页中的博客数量不为0
    @Order(2)
    @Test
    void BlogList() throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://62.234.216.147:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号jiaoao
        webDriver.findElement(By.cssSelector("#username")).sendKeys("jiaoao");
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        sleep(9000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        //跳转到博客列表页
        //获取页面上所有的博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(4)")).click();
        //用户登录信息和 博客详情信息可以对应上
        String author = webDriver.findElement(By.cssSelector("#author2")).getText();
        Assertions.assertEquals("jiaoao", author);
        System.out.println("个人列表页测试用例通过");
    }

    //测试写博客
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog) throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://62.234.216.147:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号jiaoao
        webDriver.findElement(By.cssSelector("#username")).sendKeys("jiaoao");
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        sleep(8000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(1000);
        // 点击写博客
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入文章标题
        webDriver.findElement(By.cssSelector("#title")).sendKeys(expected_title);
        sleep(4000);
        // 点击发布文章
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //找到第一篇博客对应的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
        //获取当前页面的 url  获取当前页面 title 获取博客标题
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        String cur_title = webDriver.getTitle();
        String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        if (cur_url.contains(expected_url)) {
            System.out.println("测试通过！[url正确]");
        } else {
            System.out.println("测试不通过！[url错误]");
        }
        sleep(3000);
        if (cur_title.contains("博客正文")) {
            System.out.println("测试通过！[标题正确]");
        } else {
            System.out.println("测试不通过！[标题错误]");
        }
    }


    //校验 已发布的博客标题 和 已发布博客的时间
    @Order(5)
    @Test
    void BlogInfoCheck() throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://62.234.216.147:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号jiaoao
        webDriver.findElement(By.cssSelector("#username")).sendKeys("jiaoao");
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        sleep(8000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(1000);
        //获取第一篇博客标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        //获取第一篇博客发布时间
        String first_blog_time = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.date")).getText();
        //校验博客标题是否是 ”这是自动化测试第二篇博客！“
        Assertions.assertEquals("这是自动化测试第二篇博客！", first_blog_title);
        //如果时间是 2023-08 发布的，测试通过
        if (first_blog_time.contains("2023-08")) {
            System.out.println("当前时间匹配");
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间不匹配");
            System.out.println("测试不通过！");
        }
    }

    //测试删除博客
    @Order(6)
    @Test
    void DeleteBlog() {
        //打开博客列表页面
        webDriver.get("http://62.234.216.147:8080/myblog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击删除按钮
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击 alert 中的确认删除按钮
        Alert alert = webDriver.switchTo().alert(); // 先获取到弹窗对象
        alert.accept();// 相当于点击了确认 按钮  取消按钮是dismiss
        // 博客列表页第一篇博客标题 就不是 “自动化测试”
        alert.accept();
        String first_blog_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        Assertions.assertNotEquals("自动化测试11", first_blog_title);
    }

    //测试注销功能
    @Order(7)
    @ParameterizedTest
    @MethodSource("Generator2")
    void Logout(String expected_url) {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 校验当前页面的 url 是否是博客列表页
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(expected_url, cur_url);
        // 校验页面中是否有登录按钮
        String cur_isLogin = webDriver.findElement(By.cssSelector("#loginElement")).getText();
        Assertions.assertEquals("登录", cur_isLogin);
    }

    // 测试显示个人信息功能
    @Test
    void PersonalInfoTest() {
        // 获取个人博客列表页面
        webDriver.get("http://localhost:8080/myblog_list.html");
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
        //点击查看全文后看是否显示作者信息
        String cur_info = webDriver.findElement(By.cssSelector("#author2")).getText();
        String cur_info2 = webDriver.findElement(By.cssSelector("#author")).getText();
        Assertions.assertNotNull(cur_info);
        Assertions.assertNotNull(cur_info2);
    }

    //测试文章点赞接口 这个测试用例在上一个测试用例的下一个顺序执行
    @Test
    void upvoteTest() {
        //点击主页
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        // 点击文章详情
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a")).click();
        // 获取当前点赞数量
        String upvoteCount1 = webDriver.findElement(By.cssSelector("#upvoteCount")).getText();
        Integer count1 = Integer.valueOf(upvoteCount1);
        //点击点赞按钮为用户点赞
        webDriver.findElement(By.cssSelector("#upvote > button")).click();
        //再次获取点赞数量
        String upvoteCount2 = webDriver.findElement(By.cssSelector("#upvoteCount")).getText();
        Integer count2 = Integer.valueOf(upvoteCount2);
        // 对比点赞数量是否 + 1
        Assertions.assertEquals(count1 + 1, count2);
    }

    // 测试文章热度
    @Test
    void heatTest() {
        //点击主页
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        // 获取当前文章热度
        String heat11 = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.date")).getText();
        String[] tmp11 = heat11.split(":");
        String heatCount11 = tmp11[tmp11.length-1];
        int count11 = Integer.valueOf(heatCount11);
        //点击主页
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        // 点击文章详情
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a")).click();
        // 获取当前点赞数量
        String upvoteCount1 = webDriver.findElement(By.cssSelector("#upvoteCount")).getText();
        Integer count1 = Integer.valueOf(upvoteCount1);
        //点击点赞按钮为用户点赞
        webDriver.findElement(By.cssSelector("#upvote > button")).click();
        //再次获取点赞数量
        String upvoteCount2 = webDriver.findElement(By.cssSelector("#upvoteCount")).getText();
        Integer count2 = Integer.valueOf(upvoteCount2);
        // 对比点赞数量是否 + 1
        Assertions.assertEquals(count1 + 1, count2);
        // 返回文章摘要页面
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        String heat22 = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.date")).getText();
        String[] tmp22 = heat22.split(":");
        String heatCount22 = tmp22[tmp22.length-1];
        int count22 = Integer.valueOf(heatCount22);
        Assertions.assertEquals(count11 + 1, count22);
    }

    // 测试分页功能
    @Test
    void pagingTest() {
        // 点击主页
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        // 点击首页
        webDriver.findElement(By.cssSelector("#pageDiv > button:nth-child(1)")).click();
        //再次点击首页看是否有提示页面
        webDriver.findElement(By.cssSelector("#pageDiv > button:nth-child(1)")).click();
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        String title1 = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
        // 点击下一页 看文章概要内容是否不同
        webDriver.findElement(By.cssSelector("#pageDiv > button:nth-child(3)")).click();
        String title2 = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
        Assertions.assertNotEquals(title1, title2);
        // 上一页也是同样的操作
        String title11 = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
        webDriver.findElement(By.xpath("//*[@id=\"pageDiv\"]/button[2]")).click();
        String title22 = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
        Assertions.assertNotEquals(title11, title22);
        // 点击末页
        webDriver.findElement(By.cssSelector("#pageDiv > button:nth-child(4)")).click();
        //再次点击末页
        webDriver.findElement(By.cssSelector("#pageDiv > button:nth-child(4)")).click();
        //看是否有浏览器的提示信息
        Alert alert2 = webDriver.switchTo().alert();
        alert2.accept();
    }

}
